package com.company.OfferGuide;

import com.company.TreeNode;

public class 平衡二叉树 {

    public boolean isBalanced(TreeNode root) {
        if (root == null) return true;
        return judge(root) >= 0;
    }

    public int judge(TreeNode node) {
        if (node == null) return 0;
        if (node.left == null && node.right == null) return 1;
        int left = judge(node.left);
        int right = judge(node.right);
        if (left < 0 || right < 0) return -1;
        if (Math.abs(left-right) > 1) return -1;
        return Math.max(left,right) + 1;

    }

}
